package com.yyn.Learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyn.Learning.client.OrderClient;
import com.yyn.Learning.client.UcenterClient;
import com.yyn.Learning.dto.BaseMessageDto;
import com.yyn.Learning.dto.CoursePublish;
import com.yyn.Learning.dto.front.CourseDetailDto;
import com.yyn.Learning.dto.front.CourseQueryDto;
import com.yyn.Learning.entity.*;
import com.yyn.Learning.mapper.EduCourseMapper;
import com.yyn.Learning.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.R;
import com.yyn.servicebase.exceptionhandler.CustomException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.Subject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2022-11-19
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    EduVideoService eduVideoService;
@Autowired
    OrderClient orderClient;
@Autowired
    UcenterClient ucenterClient;
    @Override
    @ApiOperation("保存基础信息")
    public void saveBase(BaseMessageDto baseMessageDto) {
//        1、创dto把简介属性加上
//        2、创方法  接收基本信息和获取到简介信息然后赋值到简介数据库中
        this.save(baseMessageDto);
        String description = baseMessageDto.getDescription();

//        存简介
//        这里需要注意到 我们的课程与简介是一对一关系 需要用id来关联
        String courseID = baseMessageDto.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(description);
        eduCourseDescription.setId(courseID);
        eduCourseDescriptionService.save(eduCourseDescription);

    }

    //    用于查老师和分类名称
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduSubjectService eduSubjectService;

    @Override
    @ApiOperation("通过id回显信息")
    public BaseMessageDto getInfoById(String id) {
        BaseMessageDto baseMessageDto = new BaseMessageDto();
//        1、获取到课程基本信息
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.eq(EduCourse::getId, id);
        EduCourse eduCourse = this.getOne(eduCourseLambdaQueryWrapper);
        BeanUtils.copyProperties(eduCourse, baseMessageDto);
//        2、获取到描述信息
        LambdaQueryWrapper<EduCourseDescription> eduCourseDescriptionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseDescriptionLambdaQueryWrapper.eq(EduCourseDescription::getId, id);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getOne(eduCourseDescriptionLambdaQueryWrapper);
        BeanUtils.copyProperties(eduCourseDescription, baseMessageDto);
//显示 所属教师
        String teacherId = eduCourse.getTeacherId();
        LambdaQueryWrapper<EduTeacher> eduTeacherLambdaQueryWrapper = new LambdaQueryWrapper<EduTeacher>();
        eduTeacherLambdaQueryWrapper.eq(EduTeacher::getId, teacherId);
        EduTeacher teacher = eduTeacherService.getOne(eduTeacherLambdaQueryWrapper);
//        由于找不到就会报错  先不要
        if (teacher != null) {
            baseMessageDto.setTeacherName(teacher.getName());
        }else {
//            throw new CustomException(20001,"该课程没有老师");
        }
        //显示 所属分类
        String subjectId = eduCourse.getSubjectId();
        LambdaQueryWrapper<EduSubject> eduSubjectLambdaQueryWrapper = new LambdaQueryWrapper<EduSubject>();
        eduSubjectLambdaQueryWrapper.eq(EduSubject::getId, subjectId);
        EduSubject subject = eduSubjectService.getOne(eduSubjectLambdaQueryWrapper);
        if (subject != null) {
            baseMessageDto.setSubjectName(subject.getTitle());
        }else {
//            throw new CustomException(20001,"该课程没有没有分类");
        }
        return baseMessageDto;
    }

    //    改
    @Override
    public void updateInfo(BaseMessageDto baseMessageDto) {
        this.updateById(baseMessageDto);
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(baseMessageDto, eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //    开始 Mabatis模式
    @Autowired
    EduCourseMapper eduCourseMapper;

    //    课程发布界面
    @Override
    public BaseMessageDto getCourse(String id) {
//        获取到了课程基本信息
        List<BaseMessageDto> course2 = eduCourseMapper.getCourse(id);
        List<BaseMessageDto> course1 = course2;
//        获取分类名
        return null;
    }

    //    课程发布
    @Override
    public CoursePublish getCoursePublish(String id) {
        CoursePublish coursePublish = eduCourseMapper.getCoursePublish(id);
        return coursePublish;
    }

    //删
    @Override
    public void removeCourseById(String courseId) {
//    根据课程id删小节
        eduVideoService.removeByCourseId(courseId);
//    根据课程id删篇章
        eduChapterService.removeByCourseId(courseId);
//    根据courseId删描述
        eduCourseDescriptionService.removeByCourseId(courseId);
//    根据courseId删课程
        boolean b = this.removeById(courseId);
        if(!b){
            throw new CustomException(20001,"删除失败");
        }
    }

    @Override
    public List<EduCourse> getRecommendCourse() {
        List<EduCourse> recommendCourse = eduCourseMapper.getRecommendCourse();
        return recommendCourse;
    }

    @Override
    public List<EduCourse> getCourseByTeacherId(String teacherId) {
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.eq(EduCourse::getTeacherId,teacherId);
        List<EduCourse> list = this.list(eduCourseLambdaQueryWrapper);
        return list;
    }

    @Override
    public Map<String, Object> getPageListFront(Page<EduCourse> eduCoursePage, CourseQueryDto courseQueryDto) {
//        1、判断所需要的条件
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(courseQueryDto.getSubjectParentId())){
            eduCourseLambdaQueryWrapper.eq(EduCourse::getSubjectParentId,courseQueryDto.getSubjectParentId());
        }
        if(StringUtils.isNotEmpty(courseQueryDto.getSubjectId())){
            eduCourseLambdaQueryWrapper.eq(EduCourse::getSubjectId,courseQueryDto.getSubjectId());
        }
        if(StringUtils.isNotEmpty(courseQueryDto.getGmtCreateSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getGmtCreate);
        }
        if(StringUtils.isNotEmpty(courseQueryDto.getPriceSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getPrice);
        }
        if(StringUtils.isNotEmpty(courseQueryDto.getBuyCountSort())){
            eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getBuyCount);
        }
        this.page(eduCoursePage,eduCourseLambdaQueryWrapper);
        List<EduCourse> records = eduCoursePage.getRecords();
        long total = eduCoursePage.getTotal();
        long pages = eduCoursePage.getPages();//一次展示多少
        long current = eduCoursePage.getCurrent();//当前页
        long size = eduCoursePage.getSize();//最大页
        boolean hasNext = eduCoursePage.hasNext();//下一页值
        boolean hasPrevious = eduCoursePage.hasPrevious();//上一页值
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

    @Override
    public CourseDetailDto getCourseDetail(String courseId,String token) {
        CourseDetailDto courseDetailDto = eduCourseMapper.getCourseDetailDto(courseId);
        R memberInfoByToken = ucenterClient.getMemberInfoByToken(token);
        HashMap<String,String> memberInfo=(HashMap<String,String>)memberInfoByToken.getData().get("memberInfo");
        String ucenterId = memberInfo.get("id");
        courseDetailDto.setCoursePayed(orderClient.checkPayCourse(courseId,ucenterId));
        return courseDetailDto;
    }

}
